package com.ipc.admin.service.gwl.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ipc.common.pojo.bo.gwl.PageAndSearchBo;
import com.ipc.common.pojo.bo.gwl.StaffBo;
import com.ipc.common.pojo.vo.R;
import com.ipc.core.dao.gwl.StaffDao;
import com.ipc.core.entity.gwl.Staff;
import com.ipc.admin.service.gwl.service.StaffService;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

/**
 * (Staff)表服务实现类
 *
 * @author 高万里
 * @since 2023-04-28 14:16:17
 */
@Service("staffService")
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffDao dao;

    @Override
    public R add(@Valid StaffBo bo) {
        if (dao.selectByNo(bo.getNo()) != null) {
            return R.fail("此编号已存在！");
        }
        Staff staff = new Staff();
        BeanUtil.copyProperties(bo, staff);

        return R.ok(dao.add(staff));
    }

    @Override
    public R del(int id) {
        if (id < 1) {
            return R.fail("参数错误，删除失败");
        }
        if (dao.deleteById(id) > 0) {
            return R.ok();
        }
        return R.fail("查询失败");
    }

    @Override
    public R delBatch(List<Integer> list) {
        return R.ok(dao.deleteBatchIds(list));
    }

    @Override
    public R update(StaffBo bo) {
        if (dao.selectByNo(bo.getNo()) != null) {
            return R.fail("此编号已存在！");
        }
        Staff staff = new Staff();
        BeanUtil.copyProperties(bo, staff);
        return R.ok(dao.updateById(staff));
    }

    @Override
    public R all() {
        return R.ok(dao.selectList(null));
    }

    //    @Override
//    public R getById(Map<String, Integer> map) {
//        if (map.get("id") == null) {
//            return R.fail("id不能为空");
//        }
//        int id = Integer.parseInt(map.get("id") + "");
//        if (id < 0) {
//            return R.fail("id不合法");
//        }
//        return R.ok(dao.getById(id));
//    }
    @Override
    public R getById(int id) {
       if (id < 1) {
           return R.fail("参数错误，查询失败");
       }
        return R.ok(dao.selectById(id));
    }

    @Override
    public R page(PageAndSearchBo bo) {
        PageHelper.startPage(bo.getPage(), bo.getSize());
        PageInfo<Staff> pageInfo = new PageInfo<>(dao.all(bo.getNo(), bo.getName()));
        return R.ok(pageInfo);
    }


    @Override
    public R export(HttpServletResponse response) {
        List<Staff> customers = dao.selectList(null);
        response.setHeader("Content-Disposition", "attachment;filename=customer" + System.currentTimeMillis() + ".xlsx");
        try {
            EasyExcel.write(response.getOutputStream(), Staff.class).sheet("Sheet1").doWrite(customers);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @Override
    public R importData(MultipartFile file) {
        if (!file.isEmpty()) {
            List<Staff> list = new ArrayList<>();
            try {
                EasyExcel.read(file.getInputStream(), Staff.class, new ReadListener<Staff>() {
                    @Override
                    public void invoke(Staff inform, AnalysisContext analysisContext) {
                        list.add(inform);
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    }
                }).sheet().doRead();
//                if (dao.addBatch(list) > 0) {
                if (dao.addBatch(list) > 0) {
                    return R.ok();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

}

